/**
 * 注册校验
 * @authors gaocc 
 * @date    2014-10-09 11:10:42
 * @update  2014-10-09 11:10:42
 * @for new_register.jsp
 * @version $Id$
 */
$(function() {
    BSTYUN.register.init();
});

var BSTYUN = BSTYUN || {};

BSTYUN.check = {
    //IE6判定
    isIE6: window.VBArray && !window.XMLHttpRequest,
    //判断是否为中英文，数字，下划线，减号
    isNick: function(str) {
        var nickReg = /^[\u4e00-\u9fa5A-Za-z0-9-_]+$/;
        return nickReg.test(str);
    },
//    //判断邮箱
//    isEmail: function(str) {
//        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
//        return emailReg.test(str);
//    },
    //判断手机.
    isMobile: function(str) {
        var mobileReg = /^1[345678][0-9]{9}$/;
        return mobileReg.test(str);
    },
    // 判断固话
    isTelephone: function(str) {
        var phoneReg = /^0\d{2,3}-\d{5,9}$/;
        return phoneReg.test(str);
    },
    //判断URL
    isUrl: function(str) {
        var urlReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/;
        return urlReg.test(str);
    },
    //判断数字
    isNum: function(str) {
        var numReg = /^[0-9]\d*$/;
        return numReg.test(str);
    }
};
BSTYUN.register = {
    $inputs: null,
    flag: { realName:false, psd: false, cfm_psd: false, picode: false},
    _timer: null,/*全局定时器*/
    init:function() {
        var that = this;
        /*获取input元素*/
        that.$inputs = $('.input_box').find('input');
        /*检测信息*/
        that.checkInfor();
        /*提交注册*/
        that.submitRegister();
        /*图片验证码替换*/
        that.changePicCode();
    },
    checkInfor:function() {
        var that = this;
        /*获取焦点，清空错误信息*/
        that.$inputs.on('focus', function(event) {
            that.clearErrorMsg($(this));
            /*失去焦点，检测信息正确性*/
        }).on('blur', function(event) {
            var $this = $(this),
                typename = $this.attr('name'),
                val = $.trim($this.val());
            /*输入内容为空，则提示不能为空*/
            if(val == '' || val.length == 0){
                that.addErrorMsg($this,'不能为空');
                return false;
            }
            if (typename == 'psd') {
                var $cfm_psd = $('#cfm_psd'),
                    cfm_psd_val = $.trim($cfm_psd.val());
                if (val.length<8 || val.length >16) {
                    that.addErrorMsg($this,'长度必须在8-16之间');
                }else{
                	/* 密码必须为数字 字母 字符的集合 */
//                    var regn = /\d/gi,
//                    	regl = /[a-zA-Z]/gi,
//                   	regz = /[`\-=~!@#$%^&*()_+\[\]\\;',.\/\\{}\|:"<>\?]/gi;
//                    if(!regn.test(val) || !regl.test(val) || !regz.test(val)){
//                    	that.addErrorMsg($this,'密码由数字、字母、字符组成');
//                    };
                };
                if (cfm_psd_val != '' || cfm_psd_val.length != 0) {
                    if (val != cfm_psd_val) {
                        that.addErrorMsg($cfm_psd,'两次输入密码不一致');
                    }else{
                        that.clearErrorMsg($cfm_psd);
                    };
                };
            }else if (typename == 'cfm_psd') {
                var psd = $.trim($('#psd').val());
                if (val != psd) {
                    that.addErrorMsg($this,'两次输入密码不一致');
                };
            }else if (typename == 'vftn_code') {
                
            }else if (typename == 'picode') {
                
            };
        });
        /*判断邮箱，是否可以发送邮件*/
    },
	changePicCode : function() {
		var $change = $("#changeImgCode"), $img = $change.prev();
		$change.on('click', function(event) {
			$img.attr('src', '/image/code?random=' + Math.random());
		});
	},
    submitRegister:function() {
        var that = this,
            $submit = $("#go2register");
        $submit.on('click', function(event) {
        	var condition = null;
            var flag = that.flag,
                data = {};
            var email = $("#email").val();
            if(email==null||email==''){
            	BSTYUN.dialog.warn("帐号未成功获取，请重新到注册页面注册");
            	return false;
            }else{
            	 var flag =  null;
            		 $.ajax({
                     type: "POST",
                     url : "/user/email?random="+Math.random(),
                     data: {userEmail:email},
                     async: false,
                     error: function(request) {
                    	 BSTYUN.dialog.ok('服务器繁忙，请稍后尝试')
                         flag = false;
                     },
                     success: function(data) {
                    	 if(data==true){
                    		 flag = true;
                    	 }else{
                    		 BSTYUN.dialog.warn("该用户已被注册，请重新注册"); 
                    		 flag = false;
                    	 }
                     }
                 });
            	if(flag==false){
            		return false ;
            	} 
            }
            $.each(that.$inputs, function(index, val) {
                 $(this).trigger('blur');
            });
            if (flag.realName != false && flag.psd != false && flag.cfm_psd != false &&  flag.picode != false ) {
                data.realName = $.trim(that.$inputs.eq(0).val());
            	data.psd = $.trim(that.$inputs.eq(1).val());
                data.cfm_psd = $.trim(that.$inputs.eq(2).val());
                data.email = email;
				data.picode = $.trim(that.$inputs.eq(3).val());
				if(data.psd.length >= 8 && data.psd.length <= 16 && data.cfm_psd.length >= 8 && data.cfm_psd.length <= 16 ){
					
				
	                $.ajax({
	                    type: "POST",
	                    url : "/user/saveUserByEmail?random="+Math.random(),
	                    data: data,
	                    async: false,
	                    error: function(request) {
	                    	BSTYUN.dialog.ok('服务器繁忙，请稍后尝试')
	                        return false;
	                    },
	                    success: function(data) {
	                        var saveSuccess = data.saveSuccess;
	                        var errorInfo = data.errorInfo;
	                        if(errorInfo == '验证码不正确'){
	                        	that.addErrorMsg(that.$inputs.filter("[name='picode']"),'验证码不正确');
	                            return false;
	                        }else {
	                        	if(saveSuccess==true){
	                        		 window.location.href = "/user/userCenter/index"
	                        	}else{
	                        		BSTYUN.dialog.warn("邮箱注册失败，请稍后重试");
	                        	}
	                        }
	                    }
	                });
				}else {
	            	BSTYUN.dialog.warn("请检查页面信息填写是否正确！");
	            	return false;
	            }
            };
            return false;
        });
    },
    changePicCode:function() {
        var $change = $("#changeImgCode"),
            $img = $change.prev();
        $change.on('click', function(event) {
            $img.attr('src','/image/code?random='+Math.random());
        });
    },
    /*清空$this的错误提示信息*/
    clearErrorMsg:function($this) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'realName') {
            that.flag.realName = true;
            $this.parent().children('.error_msg').remove();
        }
        if (typename == 'phone') {
            that.flag.phone = true;
            $this.next().children('.error_msg').remove();
        }if (typename == 'email') {
			that.flag.email = true;
			$this.parent().next().children('.error_msg').remove();
		}else if (typename == 'psd') {
            that.flag.psd = true;
            $this.parent().children('.error_msg').remove();
        }else if (typename == 'cfm_psd') {
            that.flag.cfm_psd = true;
            $this.parent().children('.error_msg').remove();
        }else if (typename == 'vftn_code') {
            that.flag.vftn_code = true;
            $this.next().next().remove();
        }else if (typename == 'picode') {
            that.flag.picode = true;
            $this.next().next().next('.error_msg').remove();
        };
    },
    /*显示错误信息*/
    addErrorMsg:function ($this,msg) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'realName') {
            that.flag.realName = false;
            if($this.next('.error_msg').size() != 0){
            	$this.next('.error_msg').html(msg);
            }else{
            	$this.after('<span class="error_msg">'+msg+'</span>');
            }
        }else if (typename == 'psd') {
            that.flag.psd = false;
            if($this.next('.error_msg').size() != 0){
            	$this.next('.error_msg').html(msg);
            }else{
            	$this.after('<span class="error_msg">'+msg+'</span>');
            }
        }else if (typename == 'cfm_psd') {
            that.flag.cfm_psd = false;
            if($this.next('.error_msg').size() != 0){
            	$this.next('.error_msg').html(msg);
            }else{
            	$this.after('<span class="error_msg">'+msg+'</span>');
            }
        }else if (typename == 'picode') {
            that.flag.picode = false;
            if($this.next().next().next('.error_msg').size() != 0){
            	$this.next().next().next('.error_msg').html(msg);
            }else{
            	$this.next().next().after('<span class="error_msg">'+msg+'</span>');
            }
        };
    },
}

/**
 * 打开百顺通云服务协议
 */
function openDeal(){
//	window.open ('/user/openBstDeal','','height=500,width=800,top=100,left=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no') 
	var openUrl = "/user/openBstDeal";//弹出窗口的url
//	var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
//	var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
	window.open (openUrl,'_blank','width='+(window.screen.availWidth-10)+',height='+(window.screen.availHeight-30)+ 
	',top=0,left=0,toolbar=yes,menubar=yes,scrollbars=yes, resizable=yes,location=yes, status=yes,channelmode=yes');
//	 window.open(openUrl,"new","toolbar=yes,menubar=yes,scrollbars=yes, resizable=yes,location=yes, status=yes)"); 
}
